﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using CMN.Controllers;
using CMN.Controllers.Filters;
using CMN.Common;
using System.Text;
using CMN.UI;
using CMN.Framework;
using CMN.Config;
namespace CMN.Controllers
{
   
    [LoggerFilter()]
    [ExceptionFilter()]
    [ExecutionTimingAttribute()]
    public class ChannelsController : Controller
    {
        [CompressFilter]
        [CacheFilterAttribute]
        public string Index(string channel, string type)
        {

            // AppLog.Write("ChannelsController-channel:" + channel, AppLog.LogMessageType.Debug);
            string path = channel;
            PageHelperEntity pageHelper = new PageHelperEntity();
            if (!string.IsNullOrEmpty(type))
            {
                if (type == "wap")
                    pageHelper.TType = TemplateType.Phone;
            }
            string[] strparam = channel.Split('_');
            string channelDir = string.Empty;
            switch (strparam.Length)
            {
                case 1:
                    {
                        channelDir = channel;
                        break;
                    }

                case 2:
                    {
                        channelDir = strparam[0];
                        pageHelper.PageIndex = Utils.StringToInt(strparam[1], 1);
                        break;
                    }
                case 3:
                    {
                        channelDir = strparam[0];
                        pageHelper.PageIndex = Utils.StringToInt(strparam[1], 1);
                        pageHelper.Order = strparam[2];
                        break;
                    }
            }
            Normal_ChannelEntity channelEntity = MvcWebCacheManager.GetNormal_ChannelByDir(channelDir);
            if (channelEntity == null)
            {
                Utils.Redirect404();
                return "";
            }
            //AppLog.Write(string.Concat(channelDir, ":", channelEntity.Title), AppLog.LogMessageType.Debug);
            pageHelper.Url = channelEntity.Url;
            pageHelper.Channel = channelEntity;
            pageHelper.ChannelId = channelEntity.Id;


            pageHelper.Title = (string.IsNullOrEmpty(channelEntity.SEOTitle) ? channelEntity.Title : channelEntity.SEOTitle);
            pageHelper.Description =  channelEntity.Info ; 
            pageHelper.Keywords = channelEntity.KeyWords;


            if (pageHelper.Channel.ReId == 0)
            {
                pageHelper.ParentDir = pageHelper.Channel.Dir;
                pageHelper.ParentChannelId = pageHelper.Channel.Id;
                pageHelper.ParentChannel = channelEntity;
            }
            else
            {
                Normal_ChannelEntity parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(pageHelper.Channel.ReId);
                if (parentChannelEntity.ReId == 0)
                {
                    pageHelper.ParentDir = parentChannelEntity.Dir;
                    pageHelper.ParentChannelId = parentChannelEntity.Id;
                    pageHelper.ParentChannel = parentChannelEntity;
                }
                else
                {
                    parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(parentChannelEntity.ReId);
                    pageHelper.ParentDir = parentChannelEntity.Dir;
                    pageHelper.ParentChannelId = parentChannelEntity.Id;
                    pageHelper.ParentChannel = parentChannelEntity;
                }
            }



            if (channelEntity.IsPart == 0 || channelEntity.IsPart == 3)
            {
                pageHelper.Type = PageType.ChannelsPage;
            }
            else if (channelEntity.IsPart == 1)
            {
                pageHelper.Type = PageType.ChannelPage;
            }
            SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
            
            if (siteInfo.IsHtml != 0 && (channelEntity.IsPart == 0 || channelEntity.IsPart == 3))
            {
                if (!string.IsNullOrEmpty(type))
                {
                    type = string.Concat(type, "\\");
                }
                string htmlPath = string.Concat(Utils.GetServerPath(), "App_Data\\html\\", type, channelEntity.Dir, ".htm");
                string content = string.Empty;
                if (Utils.FileIsOld(htmlPath, siteInfo.CacheSecond))
                {
                    content = Utils.GetFileContent(htmlPath, Encoding.GetEncoding("gb2312"));
                }
                else
                {
                    content = getIndex(pageHelper);
                    Utils.CreateFile(htmlPath, content, null);
                }
                return content;
            }
            return getIndex(pageHelper);


        }
       private string getIndex(PageHelperEntity pageHelper)
       {
           BaseTemplateDisplay articleTemplate = new BaseTemplateDisplay(pageHelper);
           string strRetrun = articleTemplate.Display();
           articleTemplate.Dispose();
           return strRetrun;
       }   
              



        
    }
}
